package com.guomao.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//@Controller
@RestController
public class ControllerC {


//    传统发送请求带参数
//    localhost:8080/mvc/test?id=100

//    restful编程风格中 参数是作为请求url路径的一部分使用
//    localhost:8080/mvc/test/100

   /* @RequestMapping("/test")
    public String test(HttpServletRequest request) {
        String id = request.getParameter("id");
        request.setAttribute("id", id);
        return "test";
    }*/


    //前端控制器会负责解析请求参数并赋值给方法入参
    /*@RequestMapping("/test")
    public String test(Integer id, Model m) {
        m.addAttribute("id", id);
        return "test";
    }*/


    //    localhost:8080/mvc/test/888
    //    请求地址/{参数名}
 /*   @RequestMapping("/test/{id}")
    public String test(@PathVariable("id") Integer id, Model m) {
        m.addAttribute("id", id);
        return "test";
    }*/


/*    @RequestMapping(value = {"/test/{id}", "/test"})
    public String test(
            @PathVariable(value = "id", required = false) Integer id,
            Model m) {
        m.addAttribute("id", id);
        return "test";
    }*/


    @GetMapping(value = "/user")
    public String getUser() {
//        return "执行查询所有用户的方法";
        return "invoke get user";
    }

    @GetMapping("/user/{id}")
    public String getUserById(@PathVariable("id") Integer id) {
//        return "执行查询id是" + id + "的用户方法";
        return "invoke get user by id:" + id;
    }

    @PostMapping("/user")
    public String addUser() {
//        return "执行新增用户的方法";
        return "invoke add user";
    }

    @DeleteMapping("/user/{id}")
    public String deleteUser(@PathVariable("id") Integer id) {
        return "执行删除id是" + id + "用户的方法";
    }


    @PutMapping("/user/{id}")
    public String updateUser(@PathVariable("id") Integer id) {
        return "执行更新id是" + id + "用户的方法";
    }
}
